﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLineNew.Controls;
using ZoneLineNew.Core;
using ZoneLineNew.Core.EventArgs;
using ZoneLineNew.Models;
using ZoneLineNew.Models.Entity.BaseInfo;
using ZoneLineNew.Services.DB;
using ZoneLineNew.Services.Message;
using ZoneLineNew.ViewModelBase;

namespace ZoneLineNew.ViewModels {
    internal class ProcessRecordViewModel : NavigationViewModel {
        public ProcessRecordViewModel(
            IRegionManager regionManager,
            IDialogService dialog,
            IMessageService messageBox,
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate
            ) : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate) {

        }


        private string searchText;

        public string SearchText {
            get { return searchText; }
            set { searchText = value; base.RaisePropertyChanged(); }
        }

        //private int _totalPage;

        //public int TotalPage {
        //    get { return _totalPage; }
        //    set { _totalPage = value; base.RaisePropertyChanged(); }
        //}

        //private int _pageIndex;

        //public int PageIndex {
        //    get { return _pageIndex; }
        //    set { _pageIndex = value; }
        //}


        private ObservableCollection<WorkSituationModel> _workSituationList = new ObservableCollection<WorkSituationModel>();

        public ObservableCollection<WorkSituationModel> WorkSituationList {
            get { return _workSituationList; }
            set { _workSituationList = value; base.RaisePropertyChanged(); }
        }

        public DelegateCommand SearchCommand => new DelegateCommand(() => {
            WorkSituationList.Clear();
            if (SearchText != "") {
                var list = _mainDbOperate.ExceSqlToList<WorkSituationModel>("select * from v_checkstationworkinfo where AbsScanCode like '%" + SearchText + "%'")?.OrderByDescending(o => o.WorkTime)?.ToList();
                if (list != null && list.Count > 0) {
                    //TotalPage = (int)Math.Ceiling(list.Count / 20.0);
                    WorkSituationList = new ObservableCollection<WorkSituationModel>(list);
                } else {
                    WorkSituationList = new ObservableCollection<WorkSituationModel>();
                }
            } else {
                _message.Error("减震器条码为空！", "温馨提示");
            }
        }); 

        public DelegateCommand<WorkSituationModel> DeleteCommand => new DelegateCommand<WorkSituationModel>((rhs) => {
            if (rhs != null) {
                var res = _message.Ask("此操作不可逆，是否确定【删除】当前工站的工作信息？", "温馨提示");
                if (res == MessageBoxResult.OK) {
                    var rlt = _mainDbOperate.ExceSql($"delete from {rhs.TableName} where AbsScanCode = '{rhs.AbsScanCode}'");
                    if (rlt) {
                        LogManager.Info($"删除{rhs.AbsScanCode},{rhs.Station}的数据删除成功！");
                        _message.GrowSuccess("删除成功！");
                    } else {
                        _message.GrowError("操作失败！");
                    }
                    SearchCommand.Execute();
                }
            }
        });

        public DelegateCommand<WorkSituationModel> ReWorkCommand => new DelegateCommand<WorkSituationModel>((rhs) => {
            if (rhs != null) {
                var res = _message.Ask("是否确定【作废】当前工站的工作信息？", "温馨提示");
                if (res == MessageBoxResult.OK) {
                    var rlt = _mainDbOperate.ExceSql($"update {rhs.TableName} set IsReworked=1 where AbsScanCode = '{rhs.AbsScanCode}'");
                    if (rlt) {
                        LogManager.Info($"{rhs.AbsScanCode},{rhs.Station}的数据作废成功！");
                        _message.GrowSuccess("作废成功！");
                    } else {
                        _message.GrowError("操作失败！");
                    }
                    SearchCommand.Execute();
                }
            }
        });


        protected override void OnViewLoaded() {
            _eventAggregator.GetEvent<PubSubEventArgs>().Unsubscribe(GetScanBarCode);
            _eventAggregator.GetEvent<PubSubEventArgs>().Subscribe(GetScanBarCode);
            //base.OnViewLoaded();
        }

        public override void OnNavigatedFrom(NavigationContext navigationContext) {
            _eventAggregator.GetEvent<PubSubEventArgs>().Unsubscribe(GetScanBarCode);
            //base.OnNavigatedFrom(navigationContext);
        }

        private void GetScanBarCode(MessageData messageData) {
            if (messageData.Code == NonClientAreaControl._scanCode && messageData.Parameters.ContainsKey("Content")) {
                this.SearchText = messageData.Parameters.GetValue<string>("Content");
            }
        }

    }
}
